package edu.biwu;

import model.Student;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @author 王俊彪
 * @version 1.0
 */
public class MybatisDemo01 {

    @Test
    public void testfindBySno(){
        // 获取到studentMapper.xml的输入流
        InputStream is = MybatisDemo01.class.getClassLoader().getResourceAsStream("sqlMapConfig.xml");
        // 构建 sqlSessionFactory工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        //通过sqlSessionFactory获取一个Session会话接口
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //通过sqlSession方法进行CRUD操作
        Student student = sqlSession.selectOne("test.findBySno", 201215122);
        System.out.println(student);

        //关闭会话
        sqlSession.close();
    }


    @Test
    public void testfindBySname(){
        // 获取到studentMapper.xml的输入流
        InputStream is = MybatisDemo01.class.getClassLoader().getResourceAsStream("sqlMapConfig.xml");
        // 构建 sqlSessionFactory工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        //通过sqlSessionFactory获取一个Session会话接口
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //通过sqlSession方法进行CRUD操作
        sqlSession.selectList("test.findBySname", "张").forEach(System.out::println);
        //关闭会话
        sqlSession.close();
    }

@Test
    public void testinsertStudent(){
        // 获取到studentMapper.xml的输入流
        InputStream is = MybatisDemo01.class.getClassLoader().getResourceAsStream("sqlMapConfig.xml");
        // 构建 sqlSessionFactory工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        //通过sqlSessionFactory获取一个Session会话接口
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //通过sqlSession方法进行CRUD操作
        try {
            Date date01 = new SimpleDateFormat("yyyy-MM-dd").parse("1996-10-12");
            java.sql.Date date02 = new java.sql.Date(date01.getTime());

            Student student = new Student("王五1", "男", date02);

            sqlSession.insert("insertStudent",student);
            sqlSession.commit();
            System.out.println(student);

        } catch (ParseException e) {
            sqlSession.rollback();
            throw new RuntimeException(e);
        }





        //关闭会话
        sqlSession.close();
    }


    @Test
    public void testupdateStudent(){
        // 获取到studentMapper.xml的输入流
        InputStream is = MybatisDemo01.class.getClassLoader().getResourceAsStream("sqlMapConfig.xml");
        // 构建 sqlSessionFactory工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        //通过sqlSessionFactory获取一个Session会话接口
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //通过sqlSession方法进行CRUD操作
        try {
            Date date01 = new SimpleDateFormat("yyyy-MM-dd").parse("1996-10-12");
            java.sql.Date date02 = new java.sql.Date(date01.getTime());

            Student student = new Student(201312348,"周芷若", "女", date02);

            sqlSession.update("test.updateStudent",student);
            sqlSession.commit();
            System.out.println(student);

        } catch (ParseException e) {
            sqlSession.rollback();
            throw new RuntimeException(e);
        }
        //关闭会话
        sqlSession.close();
    }
    @Test
    public void testdeleteStudentById(){
        // 获取到studentMapper.xml的输入流
        InputStream is = MybatisDemo01.class.getClassLoader().getResourceAsStream("sqlMapConfig.xml");
        // 构建 sqlSessionFactory工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        //通过sqlSessionFactory获取一个Session会话接口
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //通过sqlSession方法进行CRUD操作
        try {
           sqlSession.delete("test.deleteStudent",201312348);
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            throw new RuntimeException(e);
        }
        //关闭会话
        sqlSession.close();
    }
}
